package com.Order;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.Result.BaseRespones;
import com.Result.StatusCode;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mysql.cj.Session;

public class OrderInquire {
	
	/** 
	 * @apiDefine getOrderErrorMsg 订单查询失败响应信息
	 * @apiError 4001 没有找到订单
	 */
	
	/**
	 * @apiDefine getOrderSuccessMsg 订单查询成功响应信息
	 * @apiSuccess {int} code 响应码
	 * @apiSuccess {String} msg 响应信息
	 * @apiSuccess {timestamp} timestamp 时间戳
	 * @apiSuccess {ListOrders} orders 订单列表
	 */

	/**
	 * @api{Post} /Order?method=getOrder 订单查询
	 * @apiName getOrder
	 * @apiGroup Order
	 * @apiDescription 根据用户ID和订单状态进行查询 
	 * @apiDescription 订单状态码说明 
	 * 1 - 用户提交订单
	 * 2 - 用户确认
	 * 3 - 用户付钱
	 * 4 - 洗衣状态
	 *
	 * @apiParamExample {json} 居民查询请求样例
	 * {
	 *"StuID":10001,
	 *"OrderStatus":4
	 * }
	 * 
	 * 
	 * @apiUse getOrderErrorMsg
	 * @apiUse getOrderSuccessMsg
	 * 
	 * @apiSuccessExample {json} 请求成功样例
	 * {
	 * 'code':4000,
	 * 'msg':'订单查询成功',
	 * 'timestamp':'2019-06-08 22:08:42',
	 * 'data':[
	 * {
	 * 	'OrderID':10001,
	 * 	'DormID':10001,
	 * 	'WasherID':10001,
	 * 	'StuID':10001,
	 * 	'OrderStatus':4,
	 * 	'OrderMoney':5,
	 * 	'OrderPattern':1,
	 * 	'VerCode':'1234',
	 * 	'CreateTime':'2019-06-06 00:16:16',
	 * 	'ConfirmTime':'2019-06-06 00:17:16',
	 * 	'PayedTime':'2019-06-06 00:18:16',
	 * 	'FinishedTime':'2019-06-06 01:16:16'
	 * 	},
	 * 	{
	 * 	'OrderID':10004,
	 * 	'DormID':10001,
	 * 	'WasherID':10002,
	 * 	'StuID':10001,
	 * 	'OrderStatus':4,
	 * 	'OrderMoney':5,
	 * 	'OrderPattern':1,
	 * 	'VerCode':'3452',
	 * 	'CreateTime':'2019-06-06 00:16:16',
	 * 	'ConfirmTime':'2019-06-08 16:17:34',
	 * 	'PayedTime':'2019-06-09 00:25:00',
	 * 	'FinishedTime':'2019-06-08 16:26:03'
	 * 	}
	 * ]
	 * }
	 */
	public String getOrder(String requestString) throws IOException {
		String resource = "mybatisConfig.xml";
		InputStream is = org.apache.ibatis.io.Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = sessionFactory.openSession();
		// 返回对象声明
		BaseRespones response = null;
		Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
		
		//System.out.println(requestString);
		
		OrderMapper orderMapper=(OrderMapper) session.getMapper(OrderMapper.class);
		Order order=null;
		try {
			order=gson.fromJson(requestString, Order.class);
		} catch (Exception e) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		if (order==null) {
			response = new BaseRespones(StatusCode.jsonDeserializeFail);
			return gson.toJson(response);
		}
		
		List<Order> orders=orderMapper.getOrders(order);
		if (orders.isEmpty()) {
			response = new BaseRespones(StatusCode.orderInquireFail);
			return gson.toJson(response);
		}
		response = new BaseRespones(StatusCode.orderInquireSuccess);
		response.setData(orders);
		return gson.toJson(response);
	}

}
